import { NG_VALUE_ACCESSOR, NG_VALIDATORS } from '@angular/forms';
import { Component, ViewChild, forwardRef, OnInit, AfterViewChecked, Output, EventEmitter } from '@angular/core';
import { Http, Response } from '@angular/http';

import { CommonComponent } from '../../../../core/common/common.component';
import { CommonService } from '../../../../core/common/common.service';
import { ConfigService } from '../../../../core/common/config.service';
import { TableDictService } from '../../../../core/common/table-dict.service';
@Component({
  selector: 'ncloud-nodecell',
  templateUrl: 'node-cell.component.html',
  styles: [``],
  providers: [
    { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => NodeCellComponent), multi: true },
    { provide: NG_VALIDATORS, useExisting: forwardRef(() => NodeCellComponent), multi: true }
  ]
})
export class NodeCellComponent extends CommonComponent implements OnInit, AfterViewChecked {
  public select: any = {};
  @Output() public calculated: EventEmitter<any> = new EventEmitter();
  constructor(private common: CommonService, private http: Http, private tableDict: TableDictService, private config: ConfigService) {
    super();
  }
  ngOnInit() {
    super.ngOnInit();
  }
  ngAfterViewChecked(): void {

  }
  writeValue(value) {
    super.writeValue(value);
    if (value) {
      //      放在这里主要是为了初始化省份，也可以在选择节点类型后去初始化
      this.initSelect();
    }
  }
  public nodeTypeSelected($event): void {
    this.data.nodeTypeName = $event.text;
    this.tableDict.get('NODE_TYPE_' + this.data.nodeType).subscribe((data: any) => {
      this.select.nodeSubType = data;
    });
    if (!this.data.isDisabled) {
      this.data.standard = 0;
    }
    this.calculateFee(0);
  }
  public nodeSubTypeSelected($event): void {
    this.data.subTypeName = $event.text;
  }
  private initSelect(): void {
    this.tableDict.get('NODE_TYPE').subscribe((data: any) => {
      this.select.nodeType = data;
    });
  }

  public calculateFee($event: any): void {
    this.calculated.emit($event);
  }
}
